import Axios from 'axios'

const state = {
	loading: [true],		//加载动画
	tableData: [],			//单据表格数据
	menuStatus: [],			//按钮状态 1 == 刷新  2 == 删除  3 == 保存  4 == 补单  5 == 审核  6 == 附件  7 == 存为模板
	status: [],				//借款单新增页面菜单状态  3 == 保存
	rowId: null,			//修改表格行id
	loanCategoryId: [],		//借款类别id
	loanCategoryName: [],	//借款类别名称
	multipleSelection: [],	//选择项
	selectRowId: [],		//选中项的id
	bankInfo: [],			//存储付款银行信息
}

const mutations = {
	updateLoading(state, bool) {			//更新加载动画
		state.loading.splice(0, 1, bool);
	},
	updateMenuStatus(state, n) {
		state.menuStatus.splice(0, 1, n);
	},
	updateStatus(state, n) {
		state.status.splice(0, 1, n);
	},
	updateRowId(state, id) {
		state.rowId = id ? id : null;
	},
	updateLoanCategoryId(state, id) {			//更新借款类别id
		state.loanCategoryId.splice(0, 1, id);
	},
	updateBankInfo(state, obj) {
		state.bankInfo.splice(0, 1, obj);
	},
	updateLoanCategoryName(state, name) {			//更新借款类别名称
		name ? state.loanCategoryName.splice(0, 1, name) : state.loanCategoryName.splice(0);
	},
	updateTableData(state, arr) {			//更新表格数据
		let tableData = state.tableData;
		tableData.length = 0;
		arr.length > 0 ? tableData.splice(0, 1, ...arr) : tableData.splice(0);
	},
	multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
	getOrderTableDataByBtnStutas({state, commit}, url = 'activiti/showBills?billId=0&scheduleState=' + state.status[0]) {			//通过切换不同按钮状态获取对应的表格数据
		commit('updateLoading', true);
		Axios.get(url).then(res => {
			if(res.data.code == 20001) {
				setTimeout(() => {
            		if(res.data.data){
	                    commit('updateTableData', res.data.data);
	                }
	                commit('updateLoading', false);
            	}, 250);
			} else {
				setTimeout(() => {
            		commit('updateTableData', []);
	                commit('updateLoading', false);
            	}, 250);
			}
		}).catch(() => {
			setTimeout(() => {
        		commit('updateTableData', []);
                commit('updateLoading', false);
        	}, 250);
		});
	}
}


export default {
	namespaced: true,
	state,
	mutations,
	actions
}